
package com.winning.sx.microframework.controller.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/QRcode/information")
public class QRcodeController {
    @Autowired
    EasyService easyService;
    @Autowired
    ConstService service;
    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/QRcode");
        List state= service.queryCount("500");
        mv.addObject("state",state);
        return mv;
    }
    @RequestMapping(value = {"/print/{QRcodes}"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView initPrint(@PathVariable("QRcodes") String QRcodes) throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/QRcodePrint");
        mv.addObject("QRcodes",QRcodes);
        return mv;
    }
    @RequestMapping(value = {"/print/{QRcodes}/{certCodes}"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView initPrint(@PathVariable("QRcodes") String QRcodes,@PathVariable("certCodes") String certCodes) throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/QRcodePrint");
        mv.addObject("QRcodes",QRcodes);
        mv.addObject("certCodes",certCodes);
        return mv;
    }
    @RequestMapping(value = {"/printByChange/{changeId}"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView printByChange(@PathVariable("changeId") String changeId) throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/QRcodePrint");
        List codes=easyService.querySql("select q.code from BASE_Equipment e left join BASE_QRcode q  on e.QRCode=q.id " +
                "where e.changeId="+changeId,new HashMap<>()).getDataRows();
        String QRcodes="";
        for(int i=0;i<codes.size();i++){
            QRcodes+=(","+((Map)codes.get(i)).get("code").toString());
        }
        List state= service.queryCount("500");
        mv.addObject("QRcodes",QRcodes.substring(1));
        return mv;
    }
    // 二维码查询
    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
            param = objectMapper.readValue(datas, Map.class);
            DataTable all = easyService.querySql("BASE_QRcode","*",param,"","createTime desc","");
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                int num=Integer.parseInt(map.get("num"));
                for(int i=1;i<=num;i++) {
                    Map param = new HashMap();
                    param.put("state", "500002");
                    param.put("code", createValCode(12));
                    easyService.save("BASE_QRcode", "id", param);
                }
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_QRcode", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    public static String createValCode(int length) {
        String validateCode = "";
        for (int i = 0; i < length; i++) {
            validateCode += String.valueOf(Math.round(Math.random() * 8 + 1));
        }
        return validateCode;
    }
}

